package com.example.demo.until;

import java.io.IOException;
import java.net.ServerSocket;

public class PortUtils {

    /**
     * 获取一个可用的端口
     */
    public static int findAvailablePort(int defaultPort) {
        int port = defaultPort;
        try (ServerSocket socket = new ServerSocket(0)) {
            port = socket.getLocalPort(); // 获取一个随机可用端口
        } catch (IOException e) {
            e.printStackTrace();
        }
        return port;
    }

    public static void main(String[] args) {
        int availablePort = findAvailablePort(8088);
        System.out.println("可用端口：" + availablePort);
    }
}